package com.kaka.bus.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v7.widget.AppCompatButton;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.kaka.bus.AppCenter;
import com.kaka.bus.R;
import com.kaka.bus.listener.Listener;
import com.kaka.bus.util.Utils;


/**
 * Created by OnionMac on 2017/10/19.
 */

public class MessageDialog extends Dialog {

    private Activity mActivity;
    private TextView mMessageTv;
    private AppCompatButton mDialog_messagebtn;

    private Listener.OnNormalDialogLeftListener mOnNormalDialogLeftListener;

    public void setOnNormalDialogLeftListener(Listener.OnNormalDialogLeftListener onNormalDialogLeftListener) {
        mOnNormalDialogLeftListener = onNormalDialogLeftListener;
    }

    public MessageDialog(@NonNull Context context) {
        this(context, R.style.capitaldialog);
    }

    public MessageDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        mActivity = (Activity) context;
    }

    private void initView() {
        mMessageTv = (TextView) findViewById(R.id.dialog_message_tv);
        mDialog_messagebtn = (AppCompatButton) findViewById(R.id.dialog_message_btn);
        mDialog_messagebtn.setOnClickListener(v -> {
            dismiss();
            if(mOnNormalDialogLeftListener != null){
                mOnNormalDialogLeftListener.onLeft();
            }
        });
    }

    public void setMessage(String message){
        mMessageTv.setText(message);
        mMessageTv.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_message);
        setCanceledOnTouchOutside(false);

        WindowManager windowManager = mActivity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();

        Window window = getWindow();
        final WindowManager.LayoutParams lp=window.getAttributes();
        //设置居中
        window.setGravity(Gravity.CENTER);
        lp.y = Utils.dp2px(AppCenter.getInstance(),0);
        lp.alpha=1.0f;
        lp.width = (int) (display.getWidth() * 0.65);
        setCanceledOnTouchOutside(false);
        getWindow().setAttributes(lp);

        initView();
    }

}
